package manage

import "errors"

type CodeError struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
}

func (e *CodeError) Error() string {
	return e.Msg
}

func (e *CodeError) ErrorResponse() CodeError {
	return CodeError{Code: e.Code, Msg: e.Msg}
}

func NewError(code int, msg string) CodeError {
	return CodeError{Code: code, Msg: msg}
}

func DefaultErrorHandler(msg string) error {
	return &CodeError{Code: ServerError.Code, Msg: msg}
}

func ErrorHandler(err error) interface{} {
	switch {
	case errors.As(err, &err):
		return err
	default:
		return CodeError{Code: ServerError.Code, Msg: err.Error()}
	}
}
